<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 栏目分类 服务
 */
class Category_service extends MY_Service {

    const ID  = 'id';
    const PID = 'pid';

    public $cats = array();

    public function __construct() {
        parent::__construct();
        $this->load->model('category_model');
        log_message('debug', 'Category_service Service Class Initialized');
    }

    /**
     * 获取节点树
     *
     * @return array() [不带]枝叶的列表数组
     */
    public function get_tree() {
        if (empty($this->cats)) $this->cats = $this->category_model->get_all();
        $this->load->helper('tree');
        return tree_to_list(list_to_tree($this->cats, self::ID, self::PID), self::ID);
    }

    /**
     * 获取节点树（带子类）
     *
     * @return array() [带]枝叶的列表数组
     */
    public function get_trees() {
        if (empty($this->cats)) $this->cats = $this->category_model->get_all();
        $this->load->helper('tree');
        return tree_to_list(list_to_tree($this->cats, self::ID, self::PID), self::ID, true);
    }

    /**
     * 获取栏目的select/option
     *
     * @param mixed $selected 选中的id序列
     * @return string option的html
     */
    public function get_dropdown($selected = array()) {
        return $this->_biuld_dropdown($selected, $this->get_tree());
    }

    /**
     * 获取允许移动的PID数组
     * 包括顶级栏目的pid=0
     *
     * @param int $id
     * @return array
     */
    public function get_pids_allow_move($id) {
        return array_merge(array(0), array_keys($this->get_tree_allow_move($id)));
    }

    /**
     * 获取允许移动的栏目数组
     *
     * 过滤子孙分类（移动栏目时）
     *
     * @param int $id
     * @return string option的html
     */
    public function get_tree_allow_move($id) {
        static $cat_tree = array();
        if (!empty($cat_tree[$id])) return $cat_tree[$id];

        $cats = $this->category_model->get_move_dropdown($id);
        $cat_tree[$id] = tree_to_list(list_to_tree($cats, self::ID, self::PID), self::ID);
        return $cat_tree[$id];
    }

    /**
     * 获取允许移动的栏目select/option
     *
     * 过滤子孙分类（移动栏目时）
     *
     * @param mixed $selected 选中的id序列
     * @param int $id
     * @return string option的html
     */
    public function get_dropdown_allow_move($selected = array(), $id) {
        return $this->_biuld_dropdown($selected, $this->get_tree_allow_move($id));
    }

    /**
     * 组装选择框
     *
     * @param mixed $selected 选中的id序列
     * @param array $cat_tree 已排序的数组列表
     * @param bool $cover_disabled 禁止选择封面栏目
     * @return string option的html
     */
    public function _biuld_dropdown($selected, $cat_tree, $cover_disabled = false) {
        is_array($selected) || $selected = array($selected);
        $option_html = '';
        foreach ($cat_tree as $val) {
            //缩进
            $pre_html = $val['_depth'] == 0 ? '&gt;&gt; ' : str_repeat('&nbsp;', $val['_depth'] * 4) . '|- ';
            //选中
            $selected_html = in_array($val[self::ID], $selected) ? ' selected="selected" style="background:#eee;"' : '';
            //禁选
            $disabled_html = '';
            if ($cover_disabled && $val['ifpost'] == 0) {
                $disabled_html = ' disabled';
                $val['title'] .= '（封面栏目）';
            }
            //选项组装
            $option_html .= '<option value="' . $val[self::ID] . '"' . $selected_html . $disabled_html . '>';
            $option_html .= $pre_html . $val['title'] . '</option>';
        }
        return $option_html;
    }
}

/* End of file Category_service.php */
/* Location: ./application/services/doc/Category_service.php */
